#! /usr/bin/perl  -w 

use strict;

#my $usage = qq(
#Usage: $0 [--force] 'expression to calculate x value' 'expression to calculate y value'
#
#For example:  cat FOO.out | $0 "\\\$arg[0]/\\\$arg[1]" "\\\$time"
#
#will parse lines like this, dividing the first argument to the command
#by the second.
#
#CI mean was: 26.3276666666667   ../linearHashNTAThreaded        1   1000 1
#
#Which would produce this output:
#0.001\t26.3276666666667
#
#);

sub getConfig {
    my $basename = shift;
    my $key = shift;
    my $value = `grep ^$key: $basename.def`;
    $value =~ s/^$key\:\s+//;
    chomp $value;
    return $value
}


my $usage = qq(Usage: $0 foo.def\n);

my $ci_failed = qq(
The input contains a failed confidence interval.  (--force will allow the script to continue)
);

my $def = $ARGV[0];

$def =~ s/\-.+$//g;
my $defFile = "$def.def";

(-f $defFile)|| die $usage;

my $force = getConfig($def, "Force-Calc");
defined($force) || die "Required Force-Calc line missing in $defFile\n";
my $eval_x = getConfig($def, "X-Calc") 
    || die "Required X-Calc line missing in $defFile\n";
my $eval_y = getConfig($def, "Y-Calc") 
    || die "Required X-Calc line missing in $defFile\n";

my $line;

while($line = <STDIN>) {
    if($line =~ /^CI/) { 
	if ($line =~ /failed/i) {
	    if(!$force) {
		die $ci_failed;
	    } else {
		warn "Detected failed CI data point. Line was $line";
	    }
	} 
	## Remove annotation from line.
	$line =~ s/^.+\:\s+//;
	
	my @tok  = split /\s+/, $line;
	my $time = shift @tok;
	my $cmd  = shift @tok;
	my @arg  = @tok;
	
	print ((eval $eval_x) . "\t" . (eval $eval_y) . "\n");
    }
}
